/**
  ******************************************************************************
  * @file           : ScopedTimer.h
  * @author         : livio[yuanfeng1897@outlook.com]
  * @brief          : None
  * @attention      : None
  * @date           : 2023/3/2
  ******************************************************************************
  */



#ifndef CPPDEMO_SCOPEDTIMER_H
#define CPPDEMO_SCOPEDTIMER_H

#include <chrono>

// 计算单线程函数执行时间的方法
class ScopedTimer {
public:
    ScopedTimer(const char *name)
            : m_name(name),
              m_beg(std::chrono::steady_clock::now()) {}

    ~ScopedTimer() {
        auto end = std::chrono::steady_clock::now();
        auto dur = std::chrono::duration_cast<std::chrono::nanoseconds>(end - m_beg);
        std::cout << m_name << " : " << dur.count() << " ns\n";
    }

private:
    const char *m_name;
    std::chrono::time_point<std::chrono::high_resolution_clock> m_beg;
};


#endif //!CPPDEMO_SCOPEDTIMER_H
